package com.easylinkin.linkappapi.quality.controller;

import com.easylinkin.linkappapi.quality.dto.ProblemTypeDTO;
import com.easylinkin.linkappapi.quality.entity.ProblemType;
import com.easylinkin.linkappapi.quality.service.ProblemTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.List;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 问题类别管理
 */
@RestController
@RequestMapping("problemType")
@Api(tags = "问题类别管理")
public class ProblemTypeController {
  
  @Autowired
  private ProblemTypeService problemTypeService;

  /**
   * @Description: 增加问题类别
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping
  @ApiOperation("增加问题类别")
  public RestMessage insert(@RequestBody ProblemType problemType){
    //todo 参数验证
    problemTypeService.insert(problemType);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 增加问题类别,企业端调用
   * @author Tong Jie
   * @date
   */
  @PostMapping("/openapi/addFromEnterprise")
  @ApiOperation("增加问题类别")
  public RestMessage addFromEnterprise(@RequestBody ProblemType problemType) {
    problemTypeService.addFromEnterprise(problemType);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 修改问题类别,企业端调用
   * @author Tong Jie
   * @date
   */
  @PostMapping("/openapi/updateFromEnterprise")
  @ApiOperation("修改问题类别")
  public RestMessage updateFromEnterprise(@RequestBody ProblemType problemType) {
    problemTypeService.updateFromEnterprise(problemType);
    return RestBuilders.successBuilder().build();
  }


  /**
   * @Description: 删除问题类别,企业端调用
   * @author Tong Jie
   * @date
   */
  @PostMapping("/openapi/deleteFromEnterprise")
  @ApiOperation("删除问题类别")
  public RestMessage deleteFromEnterprise(@RequestBody ProblemType problemType) {
    problemTypeService.deleteFromEnterprise(problemType);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据条件，查询
   */
  @PostMapping("/openapi/listFromEnterprise")
  @ApiOperation("根据条件，查询")
  public List<ProblemTypeDTO> listFromEnterprise(@RequestBody ProblemType problemType){
    List<ProblemTypeDTO> record =  problemTypeService.listFromEnterprise(problemType);
    return record;
  }


  /**
   * @Description: 删除问题类别（包含批量删除）
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping
  @ApiOperation("删除问题类别（包含批量删除）")
  public RestMessage delBatch(@RequestBody ProblemType problemType){
    Assert.notEmpty(problemType.getIds(),"id不能为空");
    problemTypeService.removeByIds(problemType.getIds());
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 编辑问题类别
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping
  @ApiOperation("编辑问题类别")
  public RestMessage updateById(@RequestBody ProblemType problemType){
    Assert.notNull(problemType.getId(),"id不能为空");
    problemTypeService.updateOne(problemType);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据id查询问题类别详情
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("根据id查询问题类别详情")
  public RestMessage findById(@PathVariable("id")Long id) {
    Assert.notNull(id,"id不能为空");
    ProblemType problemType = problemTypeService.getById(id);
    return RestBuilders.successBuilder().data(problemType).build();
  }

  /**
   * @Description: 根据条件，查询
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，查询")
  public RestMessage queryList(@RequestBody ProblemType problemType){
    List<ProblemTypeDTO> record =  problemTypeService.queryList(problemType);
    return RestBuilders.successBuilder().data(record).build();
  }

  /**
   * @Description: 查询问题树(下拉选择使用)
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("tree")
  @ApiOperation("查询问题树(下拉选择使用)")
  public RestMessage queryTree(){
    List<ProblemTypeDTO> record =  problemTypeService.queryTree();
    return RestBuilders.successBuilder().data(record).build();
  }
}

